package com.dycx.api.rabbitmq.consumer;

import com.dycx.message.service.MessageService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * 支付监听
 */
@Slf4j
@Component
public class MessageListener {

    private static MessageService messageService;

    @Autowired
    public void MessageListener(MessageService messageService) {
        MessageListener.messageService = messageService;
    }

    /**
     * 监听消息
     *
     * @param content
     * @param channel
     * @param message
     * @throws IOException
     */
    @RabbitListener(queues = "message.create.queue", containerFactory = "messageListenerFactory")
    public void payment(@Payload String content, Channel channel, Message message) throws IOException  {
        try {
            log.info("{}============================================>收到测试消息:{}", LocalDateTime.now().toString(), content);

        } catch (Exception ex) {

            ex.printStackTrace();
        } finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }


}
